package com.dev;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import java.io.File;
public abstract class DocumentFile {
    static final String TAG = "DocumentFile";
    private final DocumentFile mParent;//nullable
    DocumentFile(DocumentFile parent) {
        mParent = parent;
    }
    public static DocumentFile fromFile(File file) {
        return new RawDocumentFile(null, file);
    }
    public static DocumentFile fromSingleUri(Context context,Uri singleUri) {
        return new SingleDocumentFile(null, context, singleUri);
    }
    public static DocumentFile fromTreeUri(Context context,Uri treeUri) {
        if (Build.VERSION.SDK_INT >= 21) {
            String documentId = DocumentsContract.getTreeDocumentId(treeUri);
            if (DocumentsContract.isDocumentUri(context, treeUri)) {
                documentId = DocumentsContract.getDocumentId(treeUri);
            }
            if (documentId == null) {
                throw new IllegalArgumentException(
                        "Could not get document ID from Uri: " + treeUri);
            }
            Uri treeDocumentUri =DocumentsContract.buildDocumentUriUsingTree(treeUri, documentId);
            if (treeDocumentUri == null) {
                throw new NullPointerException(
                        "Failed to build documentUri from a tree: " + treeUri);
            }
            return new TreeDocumentFile(null, context, treeDocumentUri);
        } else {
            return null;
        }
    }
    public static boolean isDocumentUri(@NonNull Context context, @Nullable Uri uri) {
        return DocumentsContract.isDocumentUri(context, uri);
    }
    public abstract DocumentFile createFile(@NonNull String mimeType, @NonNull String displayName);
    public abstract DocumentFile createDirectory(@NonNull String displayName);
    public abstract Uri getUri();
    public abstract String getName();
    public abstract String getType();
    public DocumentFile getParentFile() {
        return mParent;
    }
    public abstract boolean isDirectory();
    public abstract boolean isFile();
    public abstract boolean isVirtual();
    public abstract long lastModified();
    public abstract long length();
    public abstract boolean canRead();
    public abstract boolean canWrite();
    public abstract boolean delete();
    public abstract boolean exists();
    public abstract DocumentFile[] listFiles();
    public DocumentFile findFile(String displayName) {
        for (DocumentFile doc : listFiles()) {
            if (displayName.equals(doc.getName())) {
                return doc;
            }
        }
        return null;
    }
    public abstract boolean renameTo(String displayName);
}
